;
(function($, $$) {
    $.fn.moveToPos = function(options) {
        var self = this;
        var positionObj = new $$.Position(self);
        positionObj.moveToPos(options);
    }

    $$.Position = function(self, options) {
        if (!(this instanceof $$.Position)) {
            return new $$.Position(self, options);
        }
        var defaults = {

            returnOldPos : false,

            oldPos : null
        };
        this.opts = {};
        $.extend(this.opts, defaults, options);
        this.jqObj = self;
    };
    $$.Position.prototype = {

        moveToPos : function(position) {
            var positionObj = this, self = positionObj.jqObj, opts = positionObj.opts;
            var oldPos = {position : null, left : null, top : null, right : null, bottom : null};
            var returnValue;
            self.css("margin", 0);
            if (opts.returnOldPos) {
                $.each(oldPos, function(key) {
                    oldPos[key] = self.css(key);
                });
                returnValue = oldPos;
            }
            else {
                returnValue = positionObj;
            }
            if (position === undefined) {
                position = pts.position;
            }
            if (typeof position == "string") {
                setPosByStr(self, position);
            }
            else
            if ($.isPlainObject(position)) {
                setPosByObject(self, position);
            }
            return returnValue;
        }
    };


    function setPosByStr(self, posStr) {
        var windowObj = $(window);
        var parentObj = self.parent();
        if ($.prop !== undefined) {
            if (parentObj.prop("tagName").toUpperCase() == "BODY") {
                parentObj = windowObj;
            }
        }
        else
        if (parentObj.attr("tagName").toUpperCase() == "BODY") {
            parentObj = windowObj;
        }
        var windowWidth = windowObj.width(), windowHeight = windowObj.height();
        var parentWidth = parentObj.width(), parentHeight = parentObj.height();
        var targetWidth = self.outerWidth(), targetHeight = self.outerHeight();
        var posSetting = {position: "absolute", left : null, top : null, right : null, bottom : null};
        var parentOffset = self.parent().offset();
        if (self.css("position") == "fixed") {
            posSetting.position = "fixed";
        }
        switch (posStr) {
            case "center":
                parentWidth = Math.max(Math.min(parentWidth, windowWidth), targetWidth);
                parentHeight = Math.max(Math.min(parentHeight, windowHeight), targetHeight);
                posSetting["left"] = parentOffset.left + (parentWidth - targetWidth) / 2;
                posSetting["top"] = parentOffset.top + (parentHeight - targetHeight) / 2 + $(document).scrollTop();
                break;
            case "leftBottom":
                posSetting["left"] = posSetting["bottom"] = 0;
                break;
            case "leftTop":
                posSetting["left"] = posSetting["top"] = 0;
                break;
            case "rightTop":
                posSetting["right"] = posSetting["top"] = 0;
                break;
            case "rightBottom":
                posSetting["right"] = posSetting["bottom"] = 0;
                break;
            default:
                posSetting[posStr] = 0;
                break;
        }
        self.css(posSetting);
    }


    function setPosByObject(self, pos) {
        var posSetting = {position: "absolute", left : pos.left || null , top : pos.top || null, right : pos.right || null, bottom : pos.bottom || null};
        if (self.css("position") == "fixed") {
            posSetting.position = "fixed";
        }
        self.css(posSetting);
    }
})(jQuery, BASE);